<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="navbar">
      <view class="nav-left" @click="goBack">
        <text class="iconfont">&#xe603;</text>
      </view>
      <view class="nav-center">
        <text class="nav-title">打印机序列号认证</text>
      </view>
      <view class="nav-right">
        <view class="nav-icons">
          <text class="iconfont">&#xe604;</text>
        </view>
      </view>
    </view>

    <!-- 主要内容区域 -->
    <view class="main-content">
      <!-- 绑定设备信息卡片 -->
      <view class="device-info-card">
        <view class="device-icon" @click="searchPrinter">
          <image mode="aspectFill" src="/static/logo.png"></image>
        </view>
        <view class="device-text">
          <text class="title">绑定设备信息</text>
          <text class="info">设备名称：{{ deviceName }}</text>
          <text class="info">设备MAC：{{ macAddress }}</text>
          <text v-if="!deviceName" class="error-text">请先绑定打印设备</text>
        </view>
      </view>

      <!-- 设备信息输入区域 -->
      <view class="input-group">
        <view class="input-item">
          <text class="label">设备名称：</text>
          <text class="value">{{ deviceName || '暂无' }}</text>
        </view>
        <view class="input-item">
          <text class="label">MAC地址：</text>
          <text class="value">{{ macAddress || '暂无' }}</text>
        </view>
        <view class="input-item">
          <text class="label">序列号：</text>
          <text v-if="!serialNumber" class="value-placeholder">请输入序列号</text>
          <text v-else class="value">{{ serialNumber }}</text>
          <view v-if="serialNumber" class="copy-btn" @click="copySerialNumber">
            <text class="iconfont">&#xe605;</text>
          </view>
        </view>
      </view>

      <!-- 确认按钮 -->
      <view class="confirm-btn">
        <button :disabled="!serialNumber" class="btn-primary" type="primary" @click="confirm">
          确认
        </button>
      </view>


    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      deviceName: '',
      macAddress: '',
      serialNumber: ''
    }
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },

    searchPrinter() {
      // 打开打印机搜索页面
      uni.navigateTo({
        url: '/pages/njt/printerserch'
      });
    },

    copySerialNumber() {
      if (this.serialNumber) {
        uni.setClipboardData({
          data: this.serialNumber,
          success: () => {
            uni.showToast({
              title: '复制成功',
              icon: 'success'
            });
          }
        });
      }
    },

    // 在 confirm 方法中添加实际的认证请求
    confirm() {
      if (!this.serialNumber) {
        uni.showToast({
          title: '请先绑定打印机',
          icon: 'none'
        });
        return;
      }

      uni.request({
        url: '/printer/authenticate',
        method: 'POST',
        data: {
          serialNumber: this.serialNumber,
          macIp: this.macAddress
        },
        success: (res) => {
          if (res.data.code === 200) {
            uni.showToast({
              title: '认证成功',
              icon: 'success'
            });
          } else {
            uni.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          uni.showToast({
            title: '认证请求失败',
            icon: 'none'
          });
        }
      });
    },


    // 模拟从搜索页面返回的设备信息
    receiveDeviceInfo(deviceInfo) {
      this.deviceName = deviceInfo.name || '';
      this.macAddress = deviceInfo.mac || '';
      this.serialNumber = deviceInfo.serial || '';
    }
  },
  onShow() {
    // 页面显示时检查是否有保存的设备信息
    const savedDeviceInfo = uni.getStorageSync('deviceInfo');
    if (savedDeviceInfo) {
      this.deviceName = savedDeviceInfo.name;
      this.macAddress = savedDeviceInfo.mac;
      this.serialNumber = savedDeviceInfo.serial;
    }
  }
}
</script>

<style>
.container {
  background-color: #f5f5f5;
  min-height: 100vh;
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 88rpx;
  background-color: #27ae60;
  color: white;
  padding: 0 30rpx;
  position: relative;
}

.nav-left, .nav-right {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.nav-center {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  font-weight: bold;
}

.nav-icons {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60rpx;
  height: 60rpx;
}

.iconfont {
  font-size: 36rpx;
}

.main-content {
  padding: 30rpx;
}

.device-info-card {
  background-color: white;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.device-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  overflow: hidden;
  background-color: #f0f0f0;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.device-icon image {
  width: 60rpx;
  height: 60rpx;
}

.device-text {
  flex: 1;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.info {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 8rpx;
}

.error-text {
  font-size: 28rpx;
  color: #e74c3c;
  font-weight: bold;
}

.input-group {
  background-color: white;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.input-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
}

.input-item:last-child {
  border-bottom: none;
}

.label {
  font-size: 28rpx;
  color: #333;
  flex: 1;
}

.value {
  font-size: 28rpx;
  color: #333;
  flex: 1;
  text-align: right;
}

.value-placeholder {
  font-size: 28rpx;
  color: #999;
  flex: 1;
  text-align: right;
}

.copy-btn {
  width: 40rpx;
  height: 40rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #27ae60;
  cursor: pointer;
}

.confirm-btn {
  margin-bottom: 30rpx;
}

.btn-primary {
  width: 100%;
  height: 80rpx;
  background-color: #27ae60;
  color: white;
  font-size: 32rpx;
  border-radius: 16rpx;
  line-height: 80rpx;
}

.operation-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20rpx;
}

.tip-number {
  width: 30rpx;
  height: 30rpx;
  background-color: #e74c3c;
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24rpx;
  margin-right: 10rpx;
}

.tip-text {
  font-size: 28rpx;
  color: #333;
  background-color: #333;
  color: white;
  padding: 8rpx 20rpx;
  border-radius: 8rpx;
}
</style>
